<?php
	
	class stringIterator{
	
		protected $str, $len, $i;
		
		function __construct($str){
			$this->str = $str;
			$this->len = strlen($str);
			$this->i = 0;
		}
		
		function position(){
			return $this->i;	
		}
		
		function length(){
			return $this->len;	
		}
		
		function char($n){
			if($n < $this->len) return $this->str[$n];	
		}
		
		function current(){	
			return $this->char($this->i);
		}
		
		function next(){
			if($this->i < $this->len) $this->i++;
			if($this->i < $this->len) return $this->str[$this->i];
		}
		
		function prev(){
			if($this->i == 0) return;
			$this->i--;
			return $this->str[$this->i];
		}
		
		function reset(){
			$this->i = 0;	
		}
		
		function end(){
			$this->i = ($this->len-1);
		}
		
		// The following are what it's all about
		
		function lookAhead($n, $iw=false){
			$c = $this->char($this->i+$n);
			if($iw && ($c == ' ')) return $this->lookAhead($n+1, true);
			return $c;
		}
		
		function lookBehind($n, $iw=false){
			$c = $this->char($this->i-$n);
			if($iw && ($c == ' ')) return $this->lookBehind($n-1, true);
			return $c;
		}
		
		function isEscaped($n=null){
			if(is_null($n)) $n = $this->i;
			$x = 0;
			while($this->lookBehind($n-1-$x) == '\\') $x++;
			return ($x & 1);
		}
		
		function __tostring(){
			return $this->str;	
		}
	
	}
